package com.lagou.edu.controller;

import com.lagou.edu.service.CodeService;
import com.lagou.edu.service.TokenService;
import com.lagou.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    private CodeService codeService ;

    @Autowired
    private TokenService tokenService ;

    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String index(){
        return "login";
    }

    @RequestMapping("/index/register")
    public String indexRegister(){
        return "register";
    }

    /**
     * 用户注册
     * @param password
     * @param code
     * @return
     */
    @PostMapping("/register/{username}/{password}/{code}")
    @ResponseBody
    public boolean userRegister(@PathVariable(value="username") String username,@PathVariable(value = "password") String password,@PathVariable("code") String code,HttpServletResponse response){
        String token = UUID.randomUUID().toString();
        // TODO 调用验证码服务验证是否正确
        String responseCode=codeService.validateCode(username,code) ;
        if ("0".equals(responseCode)) {
            Cookie cookie = new Cookie("token",token);
            cookie.setPath("/");
            response.addCookie(cookie);
            userService.saveTokenByEmail(username,token);
            return true;
            }else if ("2".equals(responseCode)){
                return false;
            }else {
                return false;
            }
    }

    /**
     * 用户是否注册过
     * @param email
     * @return
     */
    @RequestMapping("/isRegistered/{email}")
    @ResponseBody
    public boolean isRegistered(@PathVariable String email){
        return  userService.getIsRegisteredByEmail(email);
    }

    /**
     * 用户登录
     * @param
     * @param password
     * @return
     */
    @RequestMapping("/login/{email}/{password}")
    @ResponseBody
    public String login(@PathVariable(value= "email") String username, @PathVariable(value = "password") String password, HttpServletRequest request, HttpServletResponse response){
        //根据用户明获取token
        String token=tokenService.getTokenByEmail(username) ;
        if(token==null || "".equals(token)){
            return "username or password error" ;
        }else{
            Cookie cookie = new Cookie("token",token);
            cookie.setPath("/");
            response.addCookie(cookie);
            return username + " welcome！";
        }
    }

    /**
     * 根据token获取邮件地址
     * @param token
     * @return
     */
    @RequestMapping("/info/{token}")
    @ResponseBody
    public String getEmailInfoByToken(@PathVariable String token){
        String email = userService.getEmailInfoByToken(token);
        System.out.println("邮箱地址：" + email + ";token为：" + token);
        return email;
    }
}
